<?php
namespace App\Admin\Controllers;

use App\Models\Issue;
use App\Models\Journal;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;


class IssueController extends Controller
{
    use ModelForm;


    public function index() {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    protected function grid()
    {
        return Admin::grid(Issue::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->id('ID')->sortable();
            $grid->journal()->name_short();
            $grid->year('年');
            $grid->volume('卷');
            $grid->issue('期');
            $grid->created_at();
            $grid->updated_at();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
            });
        });
    }

    protected function form()
    {
        return Admin::form(Issue::class, function (Form $form) {
            $journals = Journal::all();
            $journalsList = [];
            foreach ($journals as $j) {
                $journalsList[$j->id] = $j['name_short'];
            }
            $form->display('id', 'ID');
            $form->select('journal_id', '期刊')->options($journalsList)->rules('required');
            $form->text('title', '标题');
            $form->text('title_en', '英文标题');
            $form->number('year', '年')->default('2018')->rules('required|regex:/^\d+$/', ['regex'=>'需添年份']);
            $form->number('volume', '卷')->default('39')->rules('required|regex:/^\d+$/', ['regex'=>'需添月份']);
            $form->number('issue', '期')->default('4')->rules('required|regex:/^\d+$/', ['regex'=>'需添数字']);
            $form->text('page_start', '起始页')->default('241')->rules('required|regex:/^\d+$/', ['regex'=>'需添数字']);
            $form->text('page_end', '起始页')->default('300');
            // $form->checkbox('allow_member_download ')->options([1 => '允许会员下载']);
            $form->date('journal_created', '刊出日期')->rules('required');
            $form->date('journal_published', '发布日期');
            // $form->number('price', '每篇文章默认价格')->default('0.0');
            $form->text('journal_url', '期超链接')->rules('nullable|url');
            $form->number('number', '总期序号(即第几期)')->default('0');
            $orders = [
                '1' => '起始页',
                '2' => '顺序号',
            ];
            // $form->select('art_order', '文章排序类型')->options($orders)->default('2');
            $form->radio('is_special_issue', '是否专辑')->options(['1' => '是', '0'=> '否']);
            $form->image('cover', '封面图片');
            // $form->image('cover_en', '封面图片(英文)');
            $form->editor('content', '封面介绍');
            $form->editor('content_en', ' 封面介绍(英文) ');
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
            $form->disableViewCheck();
            $form->disableEditingCheck();
            $form->tools(function(\Encore\Admin\Form\Tools $tools) {
                $tools->disableView();
            });
        });
    }

}
